<template>
  <!-- 操作栏 -->
<!--  <el-row justify="end">-->
<!--    <el-button @click="openForm">-->
<!--      <Icon class="mr-5px" icon="clarity:contract-line" />-->
<!--      绑定物料-->
<!--    </el-button>-->
<!--  </el-row>-->
  <!-- 列表 align="center" -->
  <ContentWrap class="mt-10px">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column type="selection" width="55" />
      <el-table-column label="产品名称" align="center" prop="productName"  width="180"/>
      <!--      <el-table-column label="全名" align="center" prop="fullName"  width="140"/>-->
      <el-table-column label="产品编码" align="center" prop="productNo"  width="140"/>
      <el-table-column label="客户道具编码" align="center" prop="itemNo"  width="120"/>
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <!--      <el-table-column label="订单名称" align="center" prop="orderName" />-->
      <el-table-column label="备货单号" align="center" prop="stockNo" />
      <!--      <el-table-column label="备货单名称" align="center" prop="stockName" />-->
      <el-table-column label="操作类型" align="center" prop="operateType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_STOCK_OPERATE_TYPE" :value="scope.row.operateType" />
        </template>
      </el-table-column>
      <el-table-column label="原库存数量" align="center" prop="originCount" width="120"/>
      <el-table-column label="入库数量" align="center" prop="inCount" />
      <el-table-column label="出库数量" align="center" prop="outCount" />
      <el-table-column label="最终库存数量" align="center" prop="finalCount" width="120"/>
      <el-table-column label="说明" align="center" prop="note" width="120"/>
      <el-table-column
        label="操作时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

</template>
<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { InventoryRecordApi, InventoryRecord } from '@/api/crm/inventoryrecord'
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE } from '@/utils/dict'

defineOptions({ name: 'CrmProductList' })
const props = defineProps<{
  bizType: number // 业务类型
  bizId: number // 业务编号
  customerId: number // 业务编号
}>()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const customerId = computed(() => {
  return props.customerId;
});
const id = computed(() => {
  return props.bizId;
});

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  productId: undefined,
  productName: undefined,
  itemNo: undefined,
  productNo: undefined,
  orderNo: undefined,
  orderName: undefined,
  stockNo: undefined,
  stockName: undefined,
  operateType: undefined,
  createTime: [],
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // 置空参数
    queryParams.productId = undefined
    queryParams.productId = props.bizId
    // 执行查询
    const data = await InventoryRecordApi.getInventoryRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const { currentRoute, push } = useRouter()
const openDetail = (id: number) => {
  push({ name: 'CrmProductDetail', params: { id } })
}
/** 监听打开的 bizId + bizType，从而加载最新的列表 */
watch(
  () => [props.bizId, props.bizType],
  () => {
    handleQuery()
  },
  { immediate: true, deep: true }
)
</script>
